package com.dingding.utils;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

public class JasyptUtil {
    public static String ENC_KEY = "PCDUHXWM984Y2DSMFWET8SWPUW192F1K";

    /**
     * 加密
     */
    public static String enc(String message) {
        // 加密密钥
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(ENC_KEY));
        String result = encryptor.encrypt(message);
        return result;
    }

    public static void main(String[] args) {
        System.out.println(enc("07B086EBA2DE70BE0800"));
        System.out.println(enc("Marketing@10086"));
    }

    /**
     * 解密
     */
    public static String dec(String encMessage) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(ENC_KEY));
        String result = encryptor.decrypt(encMessage);
        return result;
    }


    /**
     * 配置
     */
    public static SimpleStringPBEConfig cryptor(String key){
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(key);
        config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }


}
